package org.flowable.app.service;

import org.flowable.app.model.runtime.CompleteFormRepresentation;
import org.flowable.app.service.exception.NotFoundException;
import org.flowable.engine.HistoryService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by zm on 17-9-25.
 */
@Service
@Transactional
public class CusFormService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CusFormService.class);

    @Autowired
    protected HistoryService historyService;

    @Autowired
    protected TaskService taskService;

    public void completeTaskForm(String taskId, CompleteFormRepresentation completeTaskFormRepresentation) {

        // Get the form definition
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();

        if (task == null) {
            throw new NotFoundException("Task not found with id: " + taskId);
        }


        taskService.completeTaskWithForm(taskId, completeTaskFormRepresentation.getFormId(),
                completeTaskFormRepresentation.getOutcome(), completeTaskFormRepresentation.getValues());
    }

}
